package ren.kane.phoenix.util.mail;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author renrk(1275) on 2018-11-02 3:37 PM
 **/
@CrossOrigin
@RestController
@RequestMapping("/phoenix/mail")
@Api(value = "Mail控制类", description = "Mail的相关接口API")
public class MailController {

    private static final String MAIL_FROM = "renrk@tiansu-china.com";

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private TemplateEngine templateEngine;

    /**
     * {
     * "content": "hello this is simple mail",
     * "subject": "test simple mail",
     * "to": "rrk_1991@163.com"
     * }
     */
    @PostMapping("/simple")
    @ApiOperation(value = "发送简单邮件", httpMethod = "POST", produces = "application/json")
    public void sendSimpleMail(@RequestBody SendMessage sendMessage) {
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(MAIL_FROM);
        simpleMailMessage.setTo(sendMessage.getTo());
        simpleMailMessage.setSubject(sendMessage.getSubject());
        simpleMailMessage.setText(sendMessage.getContent());
        try {
            javaMailSender.send(simpleMailMessage);
            System.out.println("发送简单邮件成功");
        } catch (MailException e) {
            System.out.println("发送简单邮件失败");
        }
    }

    /**
     * {
     * "content": "<html><body><h3>hello world ! 这是一封Html邮件!</h3</body></html>",
     * "subject": "test html mail",
     * "to": "rrk_1991@163.com"
     * }
     */
    @PostMapping("/html")
    @ApiOperation(value = "发送html邮件", httpMethod = "POST", produces = "application/json")
    public void sendHtmlMail(@RequestBody SendMessage sendMessage) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
            mimeMessageHelper.setFrom(MAIL_FROM);
            mimeMessageHelper.setTo(sendMessage.getTo());
            mimeMessageHelper.setSubject(sendMessage.getSubject());
            mimeMessageHelper.setText(sendMessage.getContent());
            javaMailSender.send(mimeMessage);
            System.out.println("发送html邮件成功");
        } catch (MessagingException e) {
            System.out.println("发送html邮件失败");
        }
    }

    /**
     * {
     * "content": "有附件，请查收！",
     * "filePath": "d:\\img.jpg",
     * "subject": "带附件的邮件",
     * "to": "rrk_1991@163.com"
     * }
     */
    @PostMapping("/attachment")
    @ApiOperation(value = "发送带附件邮件", httpMethod = "POST", produces = "application/json")
    public void sendAttachmentMail(@RequestBody SendMessage sendMessage) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(MAIL_FROM);
            mimeMessageHelper.setTo(sendMessage.getTo());
            mimeMessageHelper.setSubject(sendMessage.getSubject());
            mimeMessageHelper.setText(sendMessage.getContent(), true);

            String filePath = sendMessage.getFilePath();
            FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
            String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
            mimeMessageHelper.addAttachment(fileName, fileSystemResource);

            javaMailSender.send(mimeMessage);
            System.out.println("发送带附件邮件成功");
        } catch (MessagingException e) {
            System.out.println("发送带附件邮件失败");
        }
    }

    /**
     * {
     * "content": "<html><body>这是有图片的邮件：<img src='cid:001' ></body></html>",
     * "resourceId": "001",
     * "resourcePath": "d:\\img.jpg",
     * "subject": "这是有图片的邮件",
     * "to": "rrk_1991@163.com"
     * }
     */
    @PostMapping("/inline")
    @ApiOperation(value = "发送嵌入静态资源邮件", httpMethod = "POST", produces = "application/json")
    public void sendInlineResourceMail(@RequestBody SendMessage sendMessage) {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(MAIL_FROM);
            mimeMessageHelper.setTo(sendMessage.getTo());
            mimeMessageHelper.setSubject(sendMessage.getSubject());
            mimeMessageHelper.setText(sendMessage.getContent(), true);

            FileSystemResource fileSystemResource = new FileSystemResource(new File(sendMessage.getResourcePath()));
            mimeMessageHelper.addInline(sendMessage.getResourceId(), fileSystemResource);

            javaMailSender.send(mimeMessage);
            System.out.println("发送嵌入静态资源邮件成功");
        } catch (MessagingException e) {
            System.out.println("发送嵌入静态资源邮件失败");
        }
    }

    /**
     * {
     * "subject": "这是模板邮件",
     * "to": "rrk_1991@163.com"
     * }
     */
    @PostMapping("/template")
    @ApiOperation(value = "发送模板邮件", httpMethod = "POST", produces = "application/json")
    public void sendTemplateMail(@RequestBody SendMessage sendMessage) {
        Context context = new Context();
        context.setVariable("ip", "http://navi.kane.ren");
        String emailContent = templateEngine.process("emailTemplate", context);
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
            mimeMessageHelper.setFrom(MAIL_FROM);
            mimeMessageHelper.setTo(sendMessage.getTo());
            mimeMessageHelper.setSubject(sendMessage.getSubject());
            mimeMessageHelper.setText(emailContent);
            javaMailSender.send(mimeMessage);
            System.out.println("发送html邮件成功");
        } catch (MessagingException e) {
            System.out.println("发送html邮件失败");
        }
    }
}
